package com.googlecode.spruce.results;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.spruce.core.ServiceCheckResult;
import com.googlecode.spruce.dao.HostDao;
import com.googlecode.spruce.dao.ServiceDao;
import com.googlecode.spruce.dao.ServiceResultDao;
import com.googlecode.spruce.domain.Host;
import com.googlecode.spruce.domain.Service;
import com.googlecode.spruce.domain.ServiceResult;

@Transactional
public class DefaultResultsService implements ResultsService
{

    @Autowired
    private ServiceResultDao serviceResultDao;

    @Autowired
    private HostDao hostDao;

    @Autowired
    private ServiceDao serviceDao;

    @Override
    public void register(final ServiceCheckResult result)
    {
        final ServiceResult serviceResult = new ServiceResult();

        final Host host = this.hostDao.findByName(result.getHost());
        final Service service = this.serviceDao.findByName(result.getServiceCheck());

        serviceResult.setHost(host);
        serviceResult.setService(service);
        serviceResult.setCdate(result.getDate());
        serviceResult.setResult(result.getValue());
        serviceResult.setOutput(result.getMessage());

        serviceResultDao.save(serviceResult);
    }

}
